#!/usr/bin/python
#
# main.py
# Copyright (C) lcl 2012 <lcl@lcl-F-3C>
# 
# scout_report_anaylzer is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# scout_report_anaylzer is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.


from gi.repository import Gtk, GdkPixbuf, Gdk
import os, sys


#Comment the first line and uncomment the second before installing
#or making the tarball (alternatively, use project variables)
#UI_FILE = "src/scout_report_anaylzer.ui"
UI_FILE = "/usr/local/share/scout_report_anaylzer/ui/scout_report_anaylzer.ui"
#ICON = "src/icon"
ICON = "/usr/local/share/scout_report_anaylzer/ui/icon"

#to store the input data
scout_gold = 0
scout_lumber = 0
scout_rum = 0

#to store raid result
raid_gold = 0
raid_lumber = 0
raid_rum = 0

min_cap_warehouse = 1000
min_cap_rumcellar = 1000
capacity_need = 0
assumed_cap_warehouse = 1000
assumed_cap_rumcellar = 1000


# capacities of warehouse and rum cellar
# capacity_expand is for two warehouses
capacity = (0, 1000, 1100, 1551, 2101, 2838, 3663, 4576, 5775, 7150, 8800,\
	10813, 13200, 16126, 19613, 23738, 28691, 34738, 41888, 50500, 60863, 73326)
capacity_expand = (74426, 74877, 75427, 76164, 76989, 77902, 79101, 80476,\
	82126, 84139, 86526, 89452, 92939, 97064)
# flag_expand = 0 means one warehouse(or rum cellar) ,should use capacity
# flag_expand = 1 means two warehouses(or rum cellar) ,should use capacity_expand
flag_expand_warehouse = 0
flag_expand_rum_cellar = 0

	
# Function  : find the minimum capacity of warehouse and return
# FucName   : capacity_min_warehouse
# Arguments : scout_data 
#             scout_data is larger one of scout_gold and scout_lumber    
def capacity_min_warehouse(scout_data):
	if flag_expand_warehouse == 0:
		if scout_data == 0:
			return 1000
		if scout_data > 73326:
			return 73326
		for i in range(0,len(capacity)-1):
			if scout_data > capacity[i] and scout_data <= capacity[i+1]:
				return capacity[i+1]
				break
	else:
		if scout_data == 0:
			return 74426
		if scout_data > 97064:
			return 97064
		for i in range (0,len (capacity_expand)-1):
			if scout_data > capacity_expand[i] and scout_data <= capacity_expand[i+1]:
				return capacity_expand[i+1]
				break
# Function  : find the minimum capacity of rum cellar and return
# FucName   : capacity_min_rum_cellar
# Arguments : scout_data 
#             scout_data is scout_rum   
def capacity_min_rum_cellar(scout_data):
	if flag_expand_rum_cellar == 0:
		if scout_data == 0:
			return 1000
		if scout_data > 73326:
			return 73326
		for i in range(0,len(capacity)-1):
			if scout_data > capacity[i] and scout_data <= capacity[i+1]:
				return capacity[i+1]
				break
	else:
		if scout_data == 0:
			return 74426
		if scout_data > 97064:
			return 97064
		for i in range (0,len (capacity_expand)-1):
			if scout_data > capacity_expand[i] and scout_data <= capacity_expand[i+1]:
				return capacity_expand[i+1]
				break

				
# Function  : find scout_gold and scout_lumber which is large
# FucName   : large
# Arguments : scout_gold, scout_lumber
def large(scout_gold, scout_lumber):
	if scout_gold > scout_lumber :
		return scout_gold
	else:
		return scout_lumber
	
# Function  : calculate some datas
# Fucname   : calculate
def calculate():
	global scout_gold
	global scout_lumber
	global scout_rum
	global raid_gold
	global raid_lumber
	global raid_rum
	global min_cap_warehouse
	global min_cap_rumcellar
	global capacity_need
	global assumed_cap_warehouse
	global assumed_cap_rumcellar
	
	min_cap_warehouse = capacity_min_warehouse (large(scout_gold,scout_lumber))
	min_cap_rumcellar = capacity_min_rum_cellar (scout_rum)
	if min_cap_warehouse < assumed_cap_warehouse:
		cap_warehouse = assumed_cap_warehouse
	else:
		cap_warehouse = min_cap_warehouse
	if min_cap_rumcellar < assumed_cap_rumcellar:
		cap_rumcellar = assumed_cap_rumcellar
	else:
		cap_rumcellar = min_cap_rumcellar
	raid_gold = int((scout_gold - cap_warehouse / 2)*0.95)
	raid_lumber = int((scout_lumber - cap_warehouse / 2)*0.95)
	raid_rum = int((scout_rum - cap_rumcellar / 2)*0.95)
	if raid_gold < 0:
		raid_gold = 0
	if raid_lumber < 0:
		raid_lumber = 0
	if raid_rum < 0:
		raid_rum = 0
	capacity_need = int((raid_gold+raid_lumber+raid_rum)/0.95)
	
# Function  : the result text
# Fucname   : result_text
def result_text():
	global scout_gold
	global scout_lumber
	global scout_rum
	global raid_gold
	global raid_lumber
	global raid_rum
	global min_cap_warehouse
	global min_cap_rumcellar
	global capacity_need
	global assumed_cap_warehouse
	if min_cap_warehouse < assumed_cap_warehouse:
		cap_warehouse = assumed_cap_warehouse
	else:
		cap_warehouse = min_cap_warehouse
	if min_cap_rumcellar < assumed_cap_rumcellar:
		cap_rumcellar = assumed_cap_rumcellar
	else:
		cap_rumcellar = min_cap_rumcellar
	text = 'The haven\'s resouces'+'\n'+\
		'Glod '+ str(scout_gold) +\
		' Lumber '+ str(scout_lumber) +\
		' Rum '+ str(scout_rum) +\
		'\n'+'You will raid'+\
		'\n'+'Glod '+ str(raid_gold)+\
		' Lumber '+ str(raid_lumber)+\
		' Rum '+ str(raid_rum)+\
		'\n'+'Your crew\'s capacity needs ' + str(capacity_need) +\
		'\n'+'The warehouse is '+ str(cap_warehouse) +\
		'\n'+'The rum cellar is '+ str(cap_rumcellar)
	return text
		
class GUI:
	
	
	def __init__(self):

		self.builder = Gtk.Builder()
		self.builder.add_from_file(UI_FILE)
		self.builder.connect_signals(self)

		window = self.builder.get_object('window')		
		window.show_all()
		
		window.set_icon_from_file(ICON)
		

		spin1 = self.builder.get_object('spinbutton1')
		spin1.set_range(0,97064)
		spin2 = self.builder.get_object('spinbutton2')
		spin2.set_range(0,97064)
		spin3 = self.builder.get_object('spinbutton3')
		spin3.set_range(0,97064)

		combo1 = self.builder.get_object('combobox1')
		capacity_store = Gtk.ListStore(int)
		for cap in capacity:
			capacity_store.append([cap])
		combo1.set_model(capacity_store)
		renderer_text = Gtk.CellRendererText()
		combo1.pack_start(renderer_text,True)
		combo1.add_attribute(renderer_text,'text',0)

		combo2 = self.builder.get_object('combobox2')
		capacity_store = Gtk.ListStore(int)
		for cap in capacity:
			capacity_store.append([cap])
		combo2.set_model(capacity_store)
		renderer_text = Gtk.CellRendererText()
		combo2.pack_start(renderer_text,True)
		combo2.add_attribute(renderer_text,'text',0)

				
	def destroy(window, self):
		Gtk.main_quit()
		
	# press Quit callback quit the app
	def onQuitButtonClicked(self, button1):
		Gtk.main_quit()
		
	# press Calculate callback display text result in label4
	def onCalculateButtonClicked(self, label4):	
		calculate()
		label4.set_text(result_text())
		


	# if there is a userdata such as label1,
	# then the second argument means label1 object,
	# but "self" is always GUI instance.
	# and use "self.builder.get_object('spin1')" to get spin1 object
	def on_spinbutton1_changed(self, spin1):		
		global scout_gold
		text = spin1.get_text()
		if text != '':
			scout_gold = int(text)		
				
	def on_spinbutton2_changed(self, spin2):
		global scout_lumber
		text = spin2.get_text()
		if text != '':
			scout_lumber = int(text)		
			
	def on_spinbutton3_changed(self, spin3):
		global scout_rum
		text = spin3.get_text()
		if text != '':
			scout_rum = int(text)

	def on_combobox1_changed(self, combo1):
		global assumed_cap_warehouse
		tree_iter = combo1.get_active_iter()
		if tree_iter != None:
			model = combo1.get_model()
			assumed_cap_warehouse = model[tree_iter][0]


	def on_combobox2_changed(self, combo2):
		global assumed_cap_rumcellar
		tree_iter = combo2.get_active_iter()
		if tree_iter != None:
			model = combo2.get_model()
			assumed_cap_rumcellar = model[tree_iter][0]

	def on_checkbutton1_toggled(self,check1):
		global flag_expand_warehouse
		global capacity
		global capacity_expand
		global assumed_cap_warehouse
		if check1.get_active():	
			assumed_cap_warehouse = capacity_expand[0]
			flag_expand_warehouse = 1
			combo1 = self.builder.get_object('combobox1')
			capacity_store = Gtk.ListStore(int)
			for cap in capacity_expand:
				capacity_store.append([cap])
			combo1.set_model(capacity_store)
		else:
			assumed_cap_warehouse = capacity[0]
			flag_expand_warehouse = 0
			combo1 = self.builder.get_object('combobox1')
			capacity_store = Gtk.ListStore(int)
			for cap in capacity:
				capacity_store.append([cap])
			combo1.set_model(capacity_store)
						
	def on_checkbutton2_toggled (self,check2):
		global flag_expand_rum_cellar
		global capacity
		global capacity_expand
		global assumed_cap_rumcellar
		if check2.get_active():		
			assumed_cap_rumcellar = capacity_expand[0]
			flag_expand_rum_cellar = 1
			combo2 = self.builder.get_object('combobox2')
			capacity_store = Gtk.ListStore(int)
			for cap in capacity_expand:
				capacity_store.append([cap])
			combo2.set_model(capacity_store)
		else:
			assumed_cap_rumcellar = capacity[0]
			flag_expand_rum_cellar = 0
			combo2 = self.builder.get_object('combobox2')
			capacity_store = Gtk.ListStore(int)
			for cap in capacity:
				capacity_store.append([cap])
			combo2.set_model(capacity_store)
		
def main():
	app = GUI()
	Gtk.main()
		
if __name__ == "__main__":
    sys.exit(main())
